﻿using System.Data;

namespace HotelManager.DAL
{
    public class StatisticsService : IStatisticsService
    {
        private readonly HotelDbContext _context;
        public StatisticsService(HotelDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 查询每月房款收入
        /// </summary>
        /// <returns></returns>
        public double[] GetMoney(int year)
        {
            double[] money = new double[12];
            //for (int i = 0; i < 12; i++)
            //{
            //    SqlParameter[] para = { new SqlParameter("@year", year),
            //                            new SqlParameter("@month",i+1)
            //                          };
            //    object obj = SqlHelper.ExecuteScalar("usp_money", CommandType.StoredProcedure, para);
            //    if (obj == DBNull.Value)
            //    {
            //        money[i] = 0;
            //    }
            //    else
            //    {
            //        money[i] = Convert.ToDouble(obj);
            //    }
            //}
            return money;
        }

        /// <summary>
        /// 查询每月餐饮收入
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public double[] GetDishPrice(int year)
        {
            double[] price = new double[12];
            //for (int i = 0; i < 12; i++)
            //{
            //    SqlParameter[] para = { new SqlParameter("@year", year),
            //                            new SqlParameter("@month",i+1)
            //                          };
            //    object obj = SqlHelper.ExecuteScalar("usp_dishPrice", CommandType.StoredProcedure, para);
            //    if (obj == DBNull.Value)
            //    {
            //        price[i] = 0;
            //    }
            //    else
            //    {
            //        price[i] = Convert.ToDouble(obj);
            //    }
            //}
            return price;
        }

        /// <summary>
        /// 查询年度房款总收入
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public double SumTotalMoney(int year)
        {
            var v = _context.GuestRecords.Where(gr => gr.ResideDate.Year == year).Sum(gr => gr.TotalMoney);
            return decimal.ToDouble(v);

            //string sql = "select sum(TotalMoney) from GuestRecord where YEAR(LeaveDate)=@year";
            //SqlParameter[] para = { new SqlParameter("@year", year) };
            //object obj = SqlHelper.ExecuteScalar(sql, CommandType.Text, para);
            //if (obj == DBNull.Value)
            //{
            //    return 0;
            //}
            //else
            //{
            //    return Convert.ToDouble(obj);
            //}
        }

        /// <summary>
        /// 查询年度餐饮总收入
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public double SumDishMoney(int year)
        {
            var v = _context.GuestRecords.Where(gr => gr.ResideDate.Year == year).Sum(gr => gr.DishPrice);
            return decimal.ToDouble(v);

            //string sql = "select sum(DishPrice) from GuestRecord where YEAR(LeaveDate)=@year";
            //SqlParameter[] para = { new SqlParameter("@year", year) };
            //object obj = SqlHelper.ExecuteScalar(sql, CommandType.Text, para);
            //if (obj == DBNull.Value)
            //{
            //    return 0;
            //}
            //else
            //{
            //    return Convert.ToDouble(obj);
            //}
        }
    }
}
